<template>
	<view class="student-detail-container">
		<cc-navbar></cc-navbar>
		<view class="info">
			<view class="info-top fcb">
				<view class="info-top-title fc">
					<view class="u-line-1">
						{{ detail.student_name }}
					</view>
					<view class="ml10" v-if="detail.sex">
						<u-icon bold :name="detail.sex === 2 ? 'woman' : 'man'"
							:color="detail.sex === 2 ? '#d54941' : '#0052d9'" size="36rpx"></u-icon>
					</view>
				</view>
				<view v-if="detail.statusData">
					<u-text :type="detail.statusData.type" :text="detail.statusData.label"></u-text>
				</view>
			</view>
			<view class="info-bottom">
				<view class="info-bottom-row">
					<view class="info-bottom-info">
						<view class="font-info">联系方式:</view>
						<view class="u-line-1">
							<text>{{ detail.contact_phone || '-' }}</text>
							<text v-if="detail.contactRelationData">({{ detail.contactRelationData.label }})</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-sticky>
			<cc-tabs :list="tabs" :current.sync="current" :scrollable="false" />
		</u-sticky>
		<view v-show="current === 0">
			<view class="list" v-if="count">
				<block v-for="(row, index) in list" :key="index">
					<view class="item">
						<view class="item-top fcb">
							<view class="u-line-1 item-top-left">
								{{ row.course_name }}
							</view>
							<view v-if="row.statusData">
								<u-text :type="row.statusData.type" :text="row.statusData.label"></u-text>
							</view>
						</view>
						<view class="item-content">
							<view class="tags fc mb10">
								<view class="tag" v-if="row.cla_id">
									<u-tag shape="circle" size="mini" type="primary" :text="row.class_name"></u-tag>
								</view>
								<view class="tag">
									<u-tag plain plainFill shape="circle" size="mini" type="primary"
										:text="row.feeModeLabel"></u-tag>
								</view>
							</view>
							<view class="item-info">
								<view class="font-info">总课时/天:</view>
								<view class="u-line-2">{{ row.total_hour }}</view>
							</view>
							<view class="item-info">
								<view class="font-info">剩余课时/天:</view>
								<view class="u-line-2">{{ row.balance_hour }}</view>
							</view>
						</view>
					</view>
				</block>
			</view>
			<cc-loadmore :loading="loading" :current="list.length" :count="count" />
			<cc-empty v-if="!loading && !count" mode="list" />
		</view>
		<view class="classroom-info" v-show="current === 1">
			<view class="info-cell-group">
				<view class="info-cell" @tap="handleRouter(row.url)" v-for="(row, index) in menus" :key="index">
					<view class="info-cell-left">
						<text>{{ row.label }}</text>
					</view>
					<view class="info-cell-right">
						<u-icon name="arrow-right" size="24rpx" bold color="#86909c"></u-icon>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getStudentCourse
	} from '@/codecat/api/education/student/course';
	import {
		getStudent
	} from '@/codecat/api/education/student/index';
	import {
		getFeeMode,
		getDateType,
		getFeeModeUnit,
		getStudentCourseStatus
	} from '@/codecat/config';
	export default {
		// 学员详情
		name: 'StudentDetail',
		data() {
			return {
				detail: {},
				loading: false,
				list: [],
				count: 0,
				query: {
					student_id: '',
					page: 1,
					limit: 10
				},
				current: 0,
				tabs: [{
						name: '报读课程'
					},
					{
						name: '基础信息'
					}
				],
				menus: [{
						label: '上课记录',
						url: '/student-attend'
					},
					// {
					// 	label: '订单记录',
					// 	url: '/student-order',
					// },
					{
						label: '试听记录',
						url: '/student-audition'
					},
					{
						label: '跟进记录',
						url: '/student-follow'
					}
				]
			};
		},
		onReachBottom() {
			if (this.list.length < this.count && this.current === 0) {
				this.getStudentCourse();
			}
		},
		onLoad() {
			const options = this.$Route.query;
			if (options.id) {
				this.query.student_id = options.id;
				this.getStudent();
				this.getStudentCourse(1);
			}
		},
		methods: {
			getStudent() {
				getStudent({
					id: this.query.student_id
				}).then((res) => {
					if (res.data) {
						let {
							status,
							contact_relation,
							student_name
						} = res.data;
						const statusData = this.$getDict('intentionStudentStatus', status);
						const contactRelationData = this.$getDict('contactRelationOptions', contact_relation);
						this.detail = {
							...res.data,
							statusData,
							contactRelationData
						};
					} else {
						this.detail = {};
					}
				});
			},
			handleRouter(path) {
				this.$Router.push({
					path,
					query: {
						id: this.query.student_id
					}
				});
			},
			// 学员报读课程
			getStudentCourse(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				getStudentCourse(query)
					.then((res) => {
						this.loading = false;
						this.query.page = query.page;
						const {
							list,
							count
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							this.$lodash.forEach(list, (e) => {
								e.feeMode = getFeeMode(e.mode);
								e.dateType = getDateType(e.data_type);
								e.feeModeLabel = e.dateType ? `${e.feeMode}(${e.dateType})` : `${e.feeMode}`;
								e.feeModeUnit = getFeeModeUnit(e.mode, e.data_type);
								e.statusData = getStudentCourseStatus(e.status);

								const {
									mode
								} = e;
								const unit = mode === 'date' ? '(天)' : '(课时)';
								e.balance_hour = `${e.balance_hour}${unit}`;
								e.total_hour = `${e.total_hour}${unit}`;
							});
							this.list = this.list.concat(list);
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>